package byteStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class test_output {
    public static void main(String[] args) {
        /*防止出现异常,无法关流
        FileOutputStreamfos=new FileOutputStream("D:\\java\\JavaMaterial\\1.txt");
        fos.write("nihao".getBytes(StandardCharsets.UTF_8));
        fos.close();
         */
        afterjdk7();

    }
        //放在try()中需要关流的对象,自动关流.原理:实现过autocloseable接口,重写close().
    private static void afterjdk7() {
        byte []byt={98,97};
        try(FileOutputStream fos=new FileOutputStream("D:\\java\\JavaMaterial\\1.txt");){
            fos.write("nihao".getBytes(StandardCharsets.UTF_8));
            fos.write(byt,0,1);
        }catch(IOException ie){
            ie.printStackTrace();
        }
    }

    private static void beforejdk7() {
        FileOutputStream fos=null;
        try{
            fos=new FileOutputStream("D:\\java\\JavaMaterial\\1.txt");
            fos.write("nihao".getBytes(StandardCharsets.UTF_8));

        }catch (IOException ie){
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
